package com.example.user.customviewdemo.drawing.colorfilter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.example.user.customviewdemo.R;

/**
 * Created by user on 2016/5/4.
 */
public class LightingColorFilterView extends View {
    private Paint paint;
    private Bitmap bm;

    public LightingColorFilterView(Context context) {
        super(context);
    }

    public LightingColorFilterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);

        bm = BitmapFactory.decodeResource(context.getResources(), R.mipmap.blue);
    }

    public LightingColorFilterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = 500;
        int height = width*bm.getHeight()/bm.getWidth();
        canvas.drawBitmap(bm,null,new Rect(0,0,width,height),paint);

        canvas.translate(0,600);

        /**
         * public LightingColorFileter(int mul,int add)
         * mul的值为0xRRGGBB(0xffffff)   其值可以只显示纯色，如0x0000ff表示显示纯蓝色
         * add的值也为0xRRGGBB(0xffffff) 其值表示可以加深颜色值，如0x0000f0表示在蓝色值上加上f0，即加深颜色
         */
        Paint paint1 = getPaint();
        paint1.setColorFilter(new LightingColorFilter(0xff0000,0x000000));
        canvas.drawBitmap(bm,null,new Rect(0,0,width,height),paint1);
    }

    private Paint getPaint(){
        Paint paint = new Paint();
        paint.setAntiAlias(true);

        return paint;
    }
}
